angular.module('meBindingControllers',[])
  .controller('BindingCtrl',function($scope,$state,$timeout,$stateParams,$resource,ConvertedMsg,Storage,Member,Binding,ENV,Toast,Loading){

    var memberId;

    //未绑定、立即绑定
    $scope.bindList = {
      qqBindStatus : "",
      weiBoBindStatus : "",
      weiXinBindStatus : ""
    };

    //加载页面需要的数据
    $scope.$on('$ionicView.beforeEnter',function(){
      memberId = Storage.get("member").member_id;
      //加载数据库数据，获取绑定的账号状态
      Binding.getBindList(memberId);
    });
    $scope.$on('Binding.getBindList', function () {
      var result = Binding.getBindListJson();
      if(result.bindStatus){
        //对界面数据做处理
        $scope.qqBindStatus = result.ab_qq;
        $scope.weiBoBindStatus = result.ab_weibo;
        $scope.weiXinBindStatus = result.ab_weixin;

      }else{
        Toast.showMessage(result.bindMsg);
      }
    });

    //绑定QQ
    $scope.bindQqNow = function () {
      Loading.showLoading();
      //调用QQ授权
      YCQQ.checkClientInstalled(function(){
        var checkClientIsInstalled = 1;//默认值是 0,仅仅针对 iOS平台有效![]()
        YCQQ.ssoLogin(function(args){
          //将获取到的userid和token存入数据库
          Binding.bindQq(memberId,args.userid,args.access_token,"1");

        },function(failReason){
          Loading.hideLoading();
          console.log(failReason);
        },checkClientIsInstalled);
      },function(){
        // 如果安装的QQ客户端版本太低，不支持SSO登录也会返回没有安装客户端的错误
        Loading.hideLoading();
        Toast.showMessage('QQ客户端没有安装');
      });
    };
    $scope.$on('Binding.bindQq' , function () {
      var result = Binding.getBindQqJson();
      if(result.bindStatus){
        //绑定成功，更新界面
        $scope.qqBindStatus = "1";
      }
      Loading.hideLoading();
      Toast.showMessage(result.bindMsg);
    });


    //取消绑定Qq
    $scope.cancelBindQq = function () {
      Loading.showLoading();
      Binding.unbindQq(memberId,"","","0");
    };
    $scope.$on('Binding.unbindQq', function () {
      var result = Binding.getUnbindQqJson();
      if(result.bindStatus){
        $scope.qqBindStatus = "0";
      }
      Loading.hideLoading();
      Toast.showMessage("QQ解绑成功");
    });


    //绑定微博
    $scope.bindWeiBoNow = function () {
      Loading.showLoading();
      YCWeibo.checkClientInstalled(function(){
        YCWeibo.ssoLogin(function(args){
        //alert(args.access_token);
        //alert(args.userid);
        //更新绑定表
        Binding.bindWeiBo(memberId,args.userid,args.access_token,"1");
      },function(failReason){
          Loading.hideLoading();
          console.log(failReason);
      });
      },function(){
        Loading.hideLoading();
        Toast.showMessage('微博客户端没有安装');
      });
    };
    $scope.$on('Binding.bindWeiBo', function () {
      var result = Binding.getBindWeiBoJson();
      if(result.bindStatus){
        $scope.weiBoBindStatus = "1";
      }
      Loading.hideLoading();
      Toast.showMessage(result.bindMsg);
    });


    //解绑微博
    $scope.cancelBindWeiBo = function () {
      Loading.showLoading();
      Binding.unbindWeiBo(memberId,"","","0");
    };
    $scope.$on('Binding.unbindWeiBo', function () {
      var result = Binding.getUnbindWeiBoJson();
      if(result.bindStatus){
        $scope.weiBoBindStatus = "0";
      }
      Loading.hideLoading();
      Toast.showMessage("微博解绑成功");
    });



    //绑定微信
    $scope.bindWeiXinNow = function () {
      Loading.showLoading();
      Wechat.isInstalled(function (installed) {
        var scope = "snsapi_userinfo";
        Wechat.auth(scope, function (response) {
          // 需要使用返回的response.code去获取access_token
          //alert(JSON.stringify(response));
          var getCode = response.code;
          var getUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
          var getTokenUrl = $resource(getUrl);
          getTokenUrl.get(
            {
              appid: ENV.weixinAppId,
              secret:ENV.weixinAppSecret,
              code:getCode,
              grant_type:'authorization_code'
            },
            function (data) {
              //alert(JSON.stringify(data));
              var openid = data.openid;
              //更新绑定表
              Binding.bindWeiXin(memberId,openid,"1");

            }, function (err) {
              Loading.hideLoading();
              Toast.showMessage("微信绑定失败");
            });
        }, function (reason) {
          Loading.hideLoading();
          Toast.showMessage("微信绑定失败");
        });
      }, function (reason) {
        Loading.hideLoading();
        Toast.showMessage("微信绑定失败");
      });
      $timeout(function(){
        Loading.hideLoading();
      },3000);
    };
    $scope.$on('Binding.bindWeiXin', function () {
      var result = Binding.getBindWeiXinJson();
      if(result.bindStatus){
        $scope.weiXinBindStatus = "1";
      }
      Loading.hideLoading();
      Toast.showMessage(result.bindMsg);
    });


    //解绑微信
    $scope.cancelBindWeiXin = function () {
      Loading.showLoading();
      Binding.unbindWeiXin(memberId,"","0");
    };
    $scope.$on('Binding.unbindWeiXin', function () {
      var result = Binding.getUnbindWeiXinJson();
      //alert("得到后台数据："+result.bindMsg);
      if(result.bindStatus){
        $scope.weiXinBindStatus = "0";
      }
      Loading.hideLoading();
      Toast.showMessage("微信解绑成功");
    });
  });
